<template>
  <div>
    <el-form
      :inline="true"
      :model="formInline"
      class="demo-form-inline"
      size="mini"
    >
      <el-form-item label="关键字">
        <el-input v-model="formInline.keywords" placeholder="审批人"></el-input>
      </el-form-item>
      <el-form-item label="日期范围">
        <el-date-picker
          v-model="formInline.startTime"
          type="date"
          placeholder="开始日期"
        >
        </el-date-picker
        >-
        <el-date-picker
          v-model="formInline.endTime"
          type="date"
          placeholder="结束日期"
        >
        </el-date-picker>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="onSearch">查询</el-button>
        <el-button type="success" @click="addAdmin">添加</el-button>
        <el-button type="success" @click="manyDelete">批量删除</el-button>
      </el-form-item>
    </el-form>
    <el-table
      :data="tableData"
      style="width: 100%"
      @selection-change="handleSelectionChange"
    >
      <el-table-column type="selection" width="55"> </el-table-column>
      <el-table-column prop="adminId" label="用户ID" width="110">
      </el-table-column>
      <el-table-column prop="fullName" label="姓名" width="120">
      </el-table-column>
      <el-table-column prop="userName" label="用户名" width="130">
      </el-table-column>
      <el-table-column prop="email" label="邮箱" width="150"> </el-table-column>
      <el-table-column
        prop="createrTime"
        label="注册时间"
        :formatter="formatter"
      >
      </el-table-column>
      <el-table-column
        prop="lastLoginTime"
        label="末次登录时间"
        :formatter="formatterlastLoginTime"
      >
      </el-table-column>
      <el-table-column
        prop="lastLoginIP"
        label="末次登录IP"
        :formatter="formatterlastLoginIP"
      >
      </el-table-column>
      <el-table-column label="操作">
        <template slot-scope="scope">
          <el-button size="mini" @click="handleEdit(scope.$index, scope.row)"
            >编辑</el-button
          >
          <el-button
            size="mini"
            type="danger"
            @click="handleDelete(scope.$index, scope.row)"
            >删除</el-button
          >
        </template>
      </el-table-column>
    </el-table>
    <el-pagination
      background
      layout="prev, pager, next"
      :total="total"
      @current-change="handleCurrentChange"
    >
    </el-pagination>

    <el-dialog title="添加管理员" :visible.sync="dialogFormVisibleAdd" v-if="dialogFormVisibleAdd">
      <add-form ref="add" @onAddSuccess="Reload"></add-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogFormVisibleAdd = false">取 消</el-button>
        <el-button type="primary" @click="Save">确 定</el-button>
      </div>
    </el-dialog>
    <el-dialog title="编辑管理员" :visible.sync="dialogFormVisibleEdit" v-if="dialogFormVisibleEdit">
      <edit-form ref="edit" :adminId="adminId" @onAddSuccess="Reload"></edit-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogFormVisibleEdit = false">取 消</el-button>
        <el-button type="primary" @click="SaveUpdate">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { Page, Delete } from '../../apis/admin'
import AddForm from './AddForm.vue'
import EditForm from './EditForm.vue'
import moment from 'moment'
export default {
  name: 'RbacpermissionsystemUiList',
  components: {
    AddForm,
    EditForm
  },
  data () {
    return {
      adminId: 0,
      tableData: [],
      total: 0,
      pageParams: {
        pageIndex: 1
      },
      formInline: {
        keywords: '',
        startTime: '',
        endTime: ''
      },
      multipleSelection: [],
      dialogFormVisibleAdd: false,
      dialogFormVisibleEdit: false
    }
  },

  created () {},

  /** 挂载 */
  mounted () {
    this.LoadData()
  },

  methods: {
    LoadData () {
      Page(Object.assign(this.pageParams, this.formInline)).then((m) => {
        const apiData = m.data
        console.log(apiData)
        this.tableData = apiData.pageData
        this.total = apiData.count
      })
    },
    // #region
    formatter (row, column) {
      return moment(row.createrTime).format('YYYY年MM月DD日')
    },
    formatterlastLoginTime (row, column) {
      return row.lastLoginTime == null
        ? '-'
        : moment(row.lastLoginTime).format('YYYY年MM月DD日')
    },
    formatterlastLoginIP (row, column) {
      return row.lastLoginIP == null ? '-' : row.lastLoginIP
    },
    handleCurrentChange (pageIndex) {
      this.pageParams.pageIndex = pageIndex
      this.LoadData()
    },
    onSearch () {
      this.LoadData()
    },
    // #endregion
    addAdmin () {
      this.dialogFormVisibleAdd = true
    },
    Save () {
      this.$refs.add.submitForm('ruleForm')
    },
    SaveUpdate () {
      this.$refs.edit.submitForm('ruleForm')
    },
    Reload () {
      this.dialogFormVisibleAdd = false
      this.dialogFormVisibleEdit = false
      this.LoadData()
    },
    handleSelectionChange (val) {
      this.multipleSelection = val
    },
    manyDelete () {
      console.log(this.multipleSelection.map(x => {
        const obj = {
          adminId: x.adminId,
          userName: x.userName
        }
        return obj
      }))
    },
    handleEdit (index, row) {
      console.log(index, row)
      this.adminId = row.adminId
      this.dialogFormVisibleEdit = true
    },
    handleDelete (index, row) {
      const _this = this

      this.$confirm('此操作将永久删除该文件, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        Delete({ adminId: row.adminId }).then(m => {
          if (m.data) {
            this.$message({
              type: 'success',
              message: '删除成功!',
              onClose: function () {
                _this.Reload()
              }
            })
          }
        })
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消删除'
        })
      })
    }
  }
}
</script>
